function [Tsci2crtbp,Tcrtbp2sci,S_crtbp] = ECI2CRTBP_se(JD_interest,S_eci,JD_SE, Res, Ves)

%%%%% NOTE %%%%%

% S_eci coming in is the state of 
% the object of interest in ECI frame.
% For use of getting the orbit about the earth represented in the Sun-Earth
% Rotating frame. (About the barycenter of the Sun-Earth.)
%%%%%%%%%%%%%%%%


GM_sun=1.327124400*10^(11);     %Sun
GM_earth=3.986004418*10^(5);    %Earth
% GM_moon = 4.9048695*10^(3);     %Moon
%[km^3/s^2] Gravitational Parameters

r12 = 1.4959965e+8;
%[km]Distance between the Earth and the Moon.

Wcm = sqrt((GM_earth+GM_sun)/r12^3)*[0;0;1];
%[] angular velocity in Cr3bp frame

TU = 5.0226757e+6;
LU = r12;
VU = LU/TU;
%[] Canonical unit conversions

JD_difference_vector = JD_SE-JD_interest;
%[] Determine the difference time vector

ind = find(JD_difference_vector > 0 , 1 , 'first') - 1;
%[] Find the index of most close previous state

Res_o = Res(:,ind);
Ves_o = Ves(:,ind);
Ses_o = [Res_o;Ves_o];
%[km,km/s] Position and velocity of the Earth WRT Sun.

UTC_JD_interest = CalendarDate(JD_interest);
UTC_JD_closest = CalendarDate(JD_SE(ind));
%[] Get UTC time of JD time of interest, which is the JD_interest and
%JD(ind), which is the closest previous information.

time_difference_seconds = etime(UTC_JD_interest,UTC_JD_closest);
%[] Determine time difference in seconds.
if time_difference_seconds == 0
    Res_interest = Res_o;
    Ves_interest = Ves_o;
    Ses_interest = [Res_interest;Ves_interest];
else
    [~,S] = ode45(@(t,S)R2bpCartesianModel(t,S,GM_sun),[0,time_difference_seconds],Ses_o);
    S = S';
    %[] Integrate for difference amount to determine exact state of the moon
    %wrt Earth in ECI frame.
    
    Res_interest = S(1:3,end);
    Ves_interest = S(4:6,end);
    Ses_interest = [Res_interest;Ves_interest];
    %[] Save position velocity and state vector.
end

%% Determine States of the barycenter of Sun and Earth.

Scms = (GM_earth*Ses_interest)/(GM_sun+GM_earth);
Rcms = Scms(1:3);
% Vcms = Scms(4:6);
%[] Determine the state of the center of mass at JD of interest
%There are no additional terms because the Earth's position wrt Earth is
%zero vector and the gravitational parameter of the satellite is assumed to
%be zero.

%% Make all states WRT CM

Ssuncm = -Scms;
%[] state of the sun WRT cm

Searthcm = Ses_interest-Scms;
%[] state of the earth wrt cm

Teci2sci = ECI2SCI(2021);
%[] Transformation matrix from ECI 2 SCI in year 2021;

Rsate_sci = Teci2sci * S_eci(1:3);
Vsate_sci = Teci2sci * S_eci(4:6);
%[] position and velocity of the satellite WRT earth in SCI frame

Rsats_sci = Rsate_sci + Res_interest;
Vsats_sci = Vsate_sci + Ves_interest;
Ssats_sci = [Rsats_sci;Vsats_sci];
%[] position velocity and state of the spacecraft WRT SUN in SCI frame.

Ssatcm = Ssats_sci - Scms;
Rsatcm = Ssatcm(1:3);
Vsatcm = Ssatcm(4:6);
%[] Position and velocity of the spacecraft wrt CM of barycenter of Sun-Earth system

%% 4: Determine Teci2crtbp and Tcrtbp2eci

Hm = cross(Res_interest,Ves_interest);
%[1/s]Current specific angular momentum of the Moon WRT the CM in ECI coordinates.

Rhat = Res_interest / norm(Res_interest);
%[]Current CRTBP radial direction in ECI coordinates.

Nhat = Hm / norm(Hm);
%[]Current CRTBP normal direction in ECI coordinates.

That = cross(Nhat,Rhat);
%[]Current CRTBP tangential direction in ECI coordinates.

Tcrtbp2sci = [Rhat, That, Nhat];
%[]Matrix that transforms vectors from CRTBP to ECI coordinates.

Tsci2crtbp = transpose(Tcrtbp2sci);
%[] Matrix that transforms vectors from ECI to CRTBP coordinates.

%% 5: Determine S_crtbp

R_crtbp = Tsci2crtbp * Rsatcm;
%[] Determine positon of interest in CRTBP frame.

V_crtbp = Tsci2crtbp * Vsatcm - cross(Wcm,Tsci2crtbp*Rcms) - cross(Wcm,R_crtbp);
%[] Determine positon of interest in CRTBP frame.

S_crtbp = [R_crtbp/LU;V_crtbp/VU];
%[] State of the S_eci eexpressed at CRTBP frame at JD of interest.




end 